<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>轮播图</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    .banner {
      width: 500px;
      height: 300px;
      border: 1px solid black;
      margin: 0 auto;
      overflow: hidden;
    }

    ul {
      width: 100%;
      height: 100%;
      font-size: 0;
      white-space: nowrap;
      /* transition: transform 1s; */
      transform: translateX(-100%);
    }

    li {
      width: 100%;
      height: 100%;
      display: inline-block;
      font-size: 80px;
      text-align: center;
    }

    .one {
      background-color: pink;
    }

    .two {
      background-color: skyblue;
    }

    .three {
      background-color: green;
    }

    .btn {
      display: flex;
      justify-content: space-between;
    }

    .btn button {
      width: 200px;
    }
  </style>
</head>

<body>
  <div class="banner">
    <ul>
      <li class="three">3</li>
      <li class="one">1</li>
      <li class="two">2</li>
      <li class="three">3</li>
      <li class="one">1</li>
    </ul>
  </div>
  <div class="btn">
    <button class="left">左</button>
    <button class="right">右</button>
  </div>
  <script>
    //获取左右按钮和ul
    let btn_left = document.querySelector(".left");
    let btn_right = document.querySelector(".right");
    let ul = document.querySelector("ul");

    //记录点击次数，点几次就移动index*100%
    let index = 1;
    //声明一个标志位
    let isClick = false;
    ul.addEventListener("transitionend", function () {
      isClick = false;
      if (index >3) {
        //到达最右边那张时，就闪现返回正常范围
        index = 1;
        ul.style.transitionDuration = "0s";
        ul.style.transform = `translateX(-${index * 100}%)`;
      }
      if (index <1) {
        index = 3;
        ul.style.transitionDuration = "0s";
        ul.style.transform = `translateX(-${index * 100}%)`;
      }
    });
    btn_right.addEventListener("click", function () {
      if (isClick) {
        return;
      }
      isClick = true;
      index++;
      ul.style.transitionDuration = "1s";

      ul.style.transform = `translateX(-${index * 100}%)`;
     
    });

    btn_left.addEventListener("click", function () {
      if (isClick) {
        return;
      }
      isClick = true;
      index--;
      ul.style.transitionDuration = "1s";
      ul.style.transform = `translateX(-${index*100}%)`;
     
    });
  </script>
</body>

</html>